package video.controller;

import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import common.Resquest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.*;
import resgistor.Video;
import video.services.VideoOrderService;
import video.services.VideoServices;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

@FeignClient("xdclass-order-service")
@RestController
@RequestMapping("/api/video/c")
public class VideoController {
    static{
        FlowRule rule=new FlowRule();
        List<FlowRule> rules=new ArrayList<>();
        rule.setResource("getVideoOrder2");
        rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
        rule.setCount(2);
        rules.add(rule);
        com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager.loadRules(rules);
    }
    @Resource
    VideoServices videoServices;
    @Resource
    Resquest resquest;
    @PostMapping("/getVideo")
    public Resquest getVideoOrder(@RequestBody Video video){
        System.out.println(video.toString());
        if(Objects.nonNull(video)){
           return videoServices.getVideo(video.getId());
        }
        return resquest.error(401, "失败");
    }
    @GetMapping("/getVideoOrder")
    public Resquest getVideo( @RequestParam("id") Integer id){
        System.out.println("id990");
        System.out.println(id);
        return videoServices.getVideoOrder(id);
    }

   int temp=0;
    @GetMapping("/getVideoOrder2")
    public Object getVideoOrder2( @RequestParam("id") Integer id)  {
            temp++;
            if(temp%2==0){
                throw new RuntimeException();
            }

        HashMap<String,Object> map = new HashMap<>();
//        Integer i = videoServices.getVideoOrder2(id);
        map.put("map", "1");
        map.put("map", "2");
        return map;


    }
    @Autowired
    VideoOrderService videoOrderService;
    @GetMapping("/getVideoOrder3")
    public Resquest getVideoOrder3( @RequestParam("id") Integer id){
        if(id!=null){
             videoOrderService.addVideoOrder(id,"44");
             return resquest.sueccess(200, "成功", null);
        }
        return resquest.error(401, "失败");
    }
    @PostMapping("/getOrder4")
    public Resquest getOrder4(@RequestBody Video video) throws InterruptedException {
        if(Objects.nonNull(video)){

            return videoOrderService.addVideoOrder2(video);
        }
        return resquest.error(401, "失败");
    }
}

